package app.baseinfomanage.operatormanage;

import java.io.IOException;
import java.io.PrintWriter;
import java.io.UnsupportedEncodingException;
import java.math.BigDecimal;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import app.base.common.Utility;

import app.baseinfomanage.operatormanage.dao.OperatorInfoDAO;
import app.baseinfomanage.operatormanage.model.OperatorInfo;

public class OperatorFlatQuery extends HttpServlet{

	private static final long serialVersionUID = 1L;
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException 
	{
	    request.setCharacterEncoding(Utility.CHARSET_ENCODING_UTF8);
	    response.setContentType(Utility.DEFAULT_CONTENT_TYPE);

		PrintWriter out = response.getWriter(); 
		
		int start = Integer.parseInt(URLDecoder.decode(request.getParameter("start"), Utility.CHARSET_ENCODING_UTF8));
	    int limit = Integer.parseInt(URLDecoder.decode(request.getParameter("limit"), Utility.CHARSET_ENCODING_UTF8));
	    
	    String loginId = request.getParameter("loginId");
	    String operatorName = request.getParameter("operatorName");
	    String state = request.getParameter("state");
	    
    	loginId = (loginId == null || loginId.trim().length() == 0) ? "%" : URLDecoder.decode(loginId, Utility.CHARSET_ENCODING_UTF8);
    	operatorName = (operatorName == null || operatorName.trim().length() == 0) ? "%" : URLDecoder.decode(operatorName, Utility.CHARSET_ENCODING_UTF8);
    	state = (state == null || state.trim().length() == 0 || state.trim().equals("-1")) ? "%" : URLDecoder.decode(state, Utility.CHARSET_ENCODING_UTF8);
	    	    
	    Map<String, Object>  map = new HashMap<String, Object>();
	    
		map.put("start", BigDecimal.valueOf(start + 1));
		map.put("end", BigDecimal.valueOf(start + limit));
		map.put("loginId", loginId);
		map.put("operatorName", operatorName);
		map.put("state", state);
		
	    out.println(getGridListData(map).toString());
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException 
	{
		doGet(request, response);
	}
	
	//return JSON format.
	public StringBuffer getGridListData(Map<String, Object>  map) 
		throws UnsupportedEncodingException
	{
		ApplicationContext context = new ClassPathXmlApplicationContext(Utility.DEFAULT_SPRINGFRAMEWORK_APPLICATION_CONTEXT_FILE_PATH);
		OperatorInfoDAO operatorInfoDAO = (OperatorInfoDAO)context.getBean(Utility.OPERATOR_MANAGE_MODULE_BEAN_NAME);
				
		List<OperatorInfo> list = operatorInfoDAO.select_0001(map);	
		
		StringBuffer json = new StringBuffer("{totalProperty:" + operatorInfoDAO.count_0001(map) + ",root:[");
		
		for(int index = 0; index < list.size(); index++)
		{
			OperatorInfo operatorInfo = (OperatorInfo) list.get(index);
			
            json.append("{operatorId:'" + operatorInfo.getOPERATOR_ID().trim()
             	+ "',loginId:'" + operatorInfo.getLOGIN_ID().trim() 
             	+ "',operatorName:'" + operatorInfo.getOPERATOR_NAME().trim() 
            	+ "',operatorDesc:'" + operatorInfo.getOPERATOR_DESC().trim()
            	+ "',state:'" + operatorInfo.getSTATE().trim() + "'}");
            
            if (index < list.size() - 1) {
            	json.append(",");
            }
		}
		
		json.append("]}");
		
		return json;
	}
	
	/**
	 * @param args
	 * @throws UnsupportedEncodingException 
	 */
	public static void main(String[] args) throws UnsupportedEncodingException {
		// TODO Auto-generated method stub
		
	}

}